<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品-计算属性</title>
</head>
<body>    
    <div  id="app" v-clock>

        <ul>
            <li v-for="i,index in list ">
            <div v-if="(i.totalnum-i.buynum)>0">
                <div>商品-------{{i.name}}</div>
                <div><img width="100" v-bind:src="i.image"></div>
                <div>价格-------{{i.price}}</div>
                <div>剩余------{{Number(i.totalnum)-Number(i.buynum)}}</div>
                <div>购买<input v-model="i.buynum"></div>
                <button @click="deleteThing(index)">删除商品</button>
                <button @click="addThing(index)">增加商品</button>
            </div>
            </li>

    

        </ul>

    </div>
</body>

<script src="https://unpkg.com/vue@3.2.47/dist/vue.global.js"></script>


<script>
Vue.createApp({
    data:()=>{
        return{
            num:10,
            obj:{
                name:'js'
            },
            list:[
            {name:'HelloKitty猫',price:2888,totalnum:100,buynum:0,image:'https://img1.baidu.com/it/u=1032192203,1544301116&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500'},
            {name:'美乐蒂',price:2888,totalnum:100,buynum:0,image:'https://img0.baidu.com/it/u=1029514935,133898196&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500'},
            {name:'库洛米',price:2888,totalnum:100,buynum:0,image:'http://t13.baidu.com/it/u=1903623593,2772273567&fm=224&app=112&f=JPEG?w=375&h=500'}


            ]
        }
    },

    methods:{
        addThing(index){
            this.list.push(this.list[index])
        },
        deleteThing(index){
            console.log(index);
            this.list.splice(index,1)
        }
    },
    watch:{
        
        list:{
            handler(newNum,oldNum){

                console.log(newNum);
                console.log("------------------");
                console.log(oldNum);
                newNum.forEach(element => {
                    if ((element.totalnum-element.buynum)<=1) {
                        alert("购买数量超出范围")
                    }
                });
            },
            deep:true
        },



        num:function(val,oldNum){
            console.log("",val,oldNum);
        },

        
        'obj.name':function(newNum,old){
            console.log(newNum,old);
        }
    },


}).mount("#app")

</script>
</html>